package cn.enilu.guns.admin.modular.door;

import cn.enilu.guns.bean.dto.EventRecordDto;
import cn.enilu.guns.bean.entity.door.EventRecord;
import cn.enilu.guns.service.door.EventRecordService;

import cn.enilu.guns.bean.annotion.core.BussinessLog;
import cn.enilu.guns.bean.constant.factory.PageFactory;
import cn.enilu.guns.bean.dictmap.CommonDict;
import cn.enilu.guns.admin.core.base.controller.BaseController;
import cn.enilu.guns.bean.exception.GunsException;

import cn.enilu.guns.bean.vo.query.Page;
import cn.enilu.guns.bean.vo.query.SearchFilter;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/door/event/record")
public class EventRecordController extends BaseController {
	private  Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private EventRecordService eventRecordService;

	private static String PREFIX = "/door/EVENT/record/";

	/**
	* 跳转到首页
	*/
	@RequestMapping(value="",method = RequestMethod.GET)
		public String index() {
		return PREFIX + "index.html";
	}

	/**
	 * 跳转到异常记录首页
	 */
	@RequestMapping(value="/unusual",method = RequestMethod.GET)
	public String indexUnusual() {
		return PREFIX + "indexUnusual.html";
	}
	/**
	 * 跳转到正常记录首页
	 */
	@RequestMapping(value="/normal",method = RequestMethod.GET)
	public String indexNormal() {
		return PREFIX + "indexNormal.html";
	}
	/**
	* 跳转到添加页面
	*/
	@RequestMapping(value = "/add",method = RequestMethod.GET)
		public String add() {
		return PREFIX + "add.html";
	}

	/**
	* 跳转到修改页面
	*/
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable Long id, Model model) {
		EventRecord entity = eventRecordService.get(id);
		model.addAttribute("item",entity);
		return PREFIX + "edit.html";
	}
	@RequestMapping(value = "/list",method = RequestMethod.POST)
	@ResponseBody
	public Object list(@RequestParam(required = false) String name) {
		Page<EventRecord> page = new PageFactory<EventRecord>().defaultPage();
		page.addFilter("name", SearchFilter.Operator.EQ,name);
		page = eventRecordService.queryPage(page);
		return super.packForBT(page);
	}

	/**
	 * 全部记录
	 * @param userId
	 * @param equipmentName
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping(value = "/listEventRecordDto",method = RequestMethod.POST)
	@ResponseBody
	public Object listEventRecordDto(@RequestParam(required = false) Long userId,
									 @RequestParam(required = false) String equipmentName,
									 @RequestParam(required = false) String beginTime,
									 @RequestParam(required = false) String endTime) {
		Page<EventRecordDto> page = new PageFactory<EventRecordDto>().defaultPage();
		if("".equals(equipmentName)){
			equipmentName= null;
		}
		if("".equals(beginTime)){
			beginTime= null;
		}
		if("".equals(endTime)){
			endTime= null;
		}
		page = eventRecordService.queryEventRecordDtoPage(page,userId,equipmentName,beginTime,endTime,null);
		return super.packForBT(page);

//		List<DeviceInfo> deviceInfos = entranceGuardService.searchDevice();
//		return super.warpObject(new DeviceInfoWarpper(BeanUtil.objectsToMaps(deviceInfos)));
	}

	/**
	 * 正常记录
	 * @param userId
	 * @param equipmentName
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping(value = "/listNormalEventRecordDto",method = RequestMethod.POST)
	@ResponseBody
	public Object listNormalEventRecordDto(@RequestParam(required = false) Long userId,
									 @RequestParam(required = false) String equipmentName,
									 @RequestParam(required = false) String beginTime,
									 @RequestParam(required = false) String endTime) {
		Page<EventRecordDto> page = new PageFactory<EventRecordDto>().defaultPage();
		if("".equals(equipmentName)){
			equipmentName= null;
		}
		if("".equals(beginTime)){
			beginTime= null;
		}
		if("".equals(endTime)){
			endTime= null;
		}
		page = eventRecordService.queryEventRecordDtoPage(page,userId,equipmentName,beginTime,endTime,"1");
		return super.packForBT(page);

//		List<DeviceInfo> deviceInfos = entranceGuardService.searchDevice();
//		return super.warpObject(new DeviceInfoWarpper(BeanUtil.objectsToMaps(deviceInfos)));
	}

	/**
	 * 异常记录
	 * @param userId
	 * @param equipmentName
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	@RequestMapping(value = "/listUnusualEventRecordDto",method = RequestMethod.POST)
	@ResponseBody
	public Object listUnusualEventRecordDto(@RequestParam(required = false) Long userId,
										   @RequestParam(required = false) String equipmentName,
										   @RequestParam(required = false) String beginTime,
										   @RequestParam(required = false) String endTime) {
		Page<EventRecordDto> page = new PageFactory<EventRecordDto>().defaultPage();
		if("".equals(equipmentName)){
			equipmentName= null;
		}
		if("".equals(beginTime)){
			beginTime= null;
		}
		if("".equals(endTime)){
			endTime= null;
		}
		page = eventRecordService.queryEventRecordDtoPage(page,userId,equipmentName,beginTime,endTime,"0");
		return super.packForBT(page);

//		List<DeviceInfo> deviceInfos = entranceGuardService.searchDevice();
//		return super.warpObject(new DeviceInfoWarpper(BeanUtil.objectsToMaps(deviceInfos)));
	}
	/**
	* 新增事件记录表
	*/
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	@ResponseBody
	@BussinessLog(value = "新增事件记录表", key = "name",dict= CommonDict.class)
	public Object add(EventRecord eventRecord) {
		eventRecordService.insert(eventRecord);
		return SUCCESS_TIP;
	}

	/**
	* 删除事件记录表
	*/
	@RequestMapping(value = "/delete")
	@ResponseBody
	@BussinessLog(value = "删除事件记录表", key = "id",dict= CommonDict.class)
	public Object delete(@RequestParam Long id) {
		eventRecordService.delete(id);
		return SUCCESS_TIP;
	}

	/**
	* 修改事件记录表
	*/
	@RequestMapping(value = "/update")
	@ResponseBody
	@BussinessLog(value = "修改事件记录表", key = "name",dict= CommonDict.class)
	public Object update(EventRecord eventRecord) {
		eventRecordService.update(eventRecord);
		return SUCCESS_TIP;
	}

}